/****************************************************************************
  @file    : _32_BRC4C158.c
  @date    : 2020-04-07
  @brief   : 大金空调
*****************************************************************************/

/* Includes ----------------------------------------------------------------*/
#include "aircon.h"
#include "string.h"

/* Private typedef ---------------------------------------------------------*/
/* Private define ----------------------------------------------------------*/
//时间定义
#define  LEADCODE_TIME_H    (uint16_t)( 0x8000   | (uint16_t)5100 )
#define  LEADCODE_TIME_L    (uint16_t)( (~0x8000)& (uint16_t)2100 )

#define  ENDCODE_TIME_H     (uint16_t)( 0x8000   | (uint16_t)400  )
#define  ENDCODE_TIME_L       (uint16_t)( (~0x8000)& (uint16_t)30000)

#define  BIT0_TIME_H      (uint16_t)( 0x8000   | (uint16_t)400  )
#define  BIT0_TIME_L      (uint16_t)( (~0x8000)& (uint16_t)680  )
#define  BIT1_TIME_H      (uint16_t)( 0x8000   | (uint16_t)400  )
#define  BIT1_TIME_L      (uint16_t)( (~0x8000)& (uint16_t)1750  )

// 
#define BASE_1          {0x11,0xDA,0x17,0x18,0x04,0x00,0x1E}
#define BASE_2          {0x11,0xDA,0x17,0x18,0x00,0x53,0x00,0x20,0x0F,0x0F,0x10,0x36,0x00,0x20,0x00}


/* Private macro -----------------------------------------------------------*/
/* Private variables -------------------------------------------------------*/
static uint16_t Sendlen = 0;                //发送帧组包长度
static const uint8_t byte_1[7] = BASE_1;

/* Private function prototypes ---------------------------------------------*/
/* Private functions -------------------------------------------------------*/
static void PackageByte(uint8_t byte)
{
  uint8_t i;

  for(i = 0; i < 8; ++i) {
    if(byte&(0x01<<(i))) {  //高电平
      IrSendBuf[Sendlen++] = BIT1_TIME_H;
      IrSendBuf[Sendlen++] = BIT1_TIME_L;
    } else {        //低电平
      IrSendBuf[Sendlen++] = BIT0_TIME_H;
      IrSendBuf[Sendlen++] = BIT0_TIME_L;
    }
  }
}

/* Public functions --------------------------------------------------------*/
void BRC4C158IrPackage(void *p)
{
  uint8_t i,last_onoff = 0,last_mode = 0;
  
  uint8_t byte_2[15] = BASE_2;

  uint8_t AirConOnOff = ((uint8_t*)p)[0];
  uint8_t AirConMode = ((uint8_t*)p)[1];
  uint8_t AirConFan = ((uint8_t*)p)[2];
  uint8_t AirConTmp = ((uint8_t*)p)[3];

  // 设定温度
  if(AirConTmp > 32)  AirConTmp = 32;
  else if(AirConTmp < 16)  AirConTmp = 16;
  byte_2[10] = (AirConTmp - 16)*2 + 14;
  
  if(last_onoff == AirConOnOff && last_mode != AirConMode) byte_2[6] = 0x04;
  
  // 
  if(AirConOnOff == AIRCON_ON) {        //开空调
    byte_2[7] = 0x01;
  } else if(AirConOnOff == AIRCON_OFF) {    //关空调
    byte_2[7] = 0x00;
  }

  // 
  if(AirConMode == AIRCON_COOLING) {      //制冷
    byte_2[7] |= 0x20;
  } else if(AirConMode == AIRCON_HEATING) { //制热
    byte_2[7] |= 0x10;
  } else {  //通风
    AirConMode == AIRCON_WENTING;
    byte_2[5] = 0x43;
    byte_2[10] = 0x10;
  }

  // 
  if(AirConFan == AIRCON_WINDLOW)     //低速
    byte_2[11] = 0x16;
  else if(AirConFan == AIRCON_WINDMID)  //中速
    byte_2[11] = 0x36;
  else if(AirConFan == AIRCON_WINDHIG)  //高速
    byte_2[11] = 0x56;
    
  // 
  last_onoff = AirConOnOff;
  last_mode = AirConMode;
  
  // 校验
  for(i = 0; i < 14; ++i) {
    byte_2[14] += byte_2[i];
  }
  
  ////
  //while(IrDev.State != IRREMOTE_IDLE);  //等待发送完成

  memset(IrSendBuf, 0, sizeof(IrSendBuf)/sizeof(uint16_t));

  Sendlen = 0;

  //配置引导码
  IrSendBuf[Sendlen++] = LEADCODE_TIME_H;
  IrSendBuf[Sendlen++] = LEADCODE_TIME_L;

  for(i = 0; i < 7; ++i) {
    PackageByte(byte_1[i]);
  }

  IrSendBuf[Sendlen++] = ENDCODE_TIME_H;
  IrSendBuf[Sendlen++] = ENDCODE_TIME_L;
  
  IrSendBuf[Sendlen++] = LEADCODE_TIME_H;
  IrSendBuf[Sendlen++] = LEADCODE_TIME_L;

  for(i = 0; i < 15; ++i) {
    PackageByte(byte_2[i]);
  }
  
  IrSendBuf[Sendlen++] = ENDCODE_TIME_H;
  IrSendBuf[Sendlen++] = ENDCODE_TIME_L;
  IrSendBuf[Sendlen++] = ENDCODE_TIME_L;
  IrSendBuf[Sendlen++] = ENDCODE_TIME_L;
  IrSendBuf[Sendlen++] = ENDCODE_TIME_L;
  IrSendBuf[Sendlen++] = ENDCODE_TIME_L;
  IrSendBuf[Sendlen++] = ENDCODE_TIME_L;
  
  //组包完成提交
  IrDev.Ptr     = IrSendBuf;
  IrDev.Len     = Sendlen;
  IrDev.State   = IRREMOTE_REQUIRE;
}
